package userhandler

import (
	"github.com/gin-gonic/gin"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// 获取分公司用户列表
func HandleGetBranchUserList(ctx *gin.Context) {
	// 请求参数处理
	param := &request.UserList{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	param.ParentId = 2 // 默认为
	param.IsAdmin = helper.TypeNumIsAdminBranch
	//param.IsPlace = 3

	data := dao.NewUserDao().GetBranchUserList(param)

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// 获取分公司用户列表
func HandleGetBranchUserListNoPager(ctx *gin.Context) {
	// 请求参数处理
	param := &request.UserList{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	param.PageNo = 0
	param.PageSize = 0
	param.ParentId = 2
	param.IsAdmin = helper.TypeNumIsAdminBranch
	//param.IsPlace = 3

	Response(ctx)("", CodeSuccess,
		dao.NewUserDao().GetBranchUserList(param).Unwrap(),
	)(OK)
}

// 新增
func HandleInsertBranchUser(ctx *gin.Context) {
	param := &request.AdminUserInsertReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	// 获取roleId
	//lists := make([]*roles.RolesModel, 0)
	//lists = dao.NewRoleDao().GetRoleListByIsAdmin(int(param.IsAdmin)).Unwrap().([]*roles.RolesModel)
	//for _, lst := range lists {
	//	param.RoleId = lst.Id
	//}
	Response(ctx)("新增成功", CodeSuccess, dao.NewUserDao().InsertBranchUser(param).Unwrap())(OK)
}

// 修改
func HandleUpdateBranchUser(ctx *gin.Context) {
	param := &request.AdminUserInsertReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	Response(ctx)("", CodeUpdateSuccess, dao.NewUserDao().UpdateBranchUser(param).Unwrap())(OK)
}
